﻿#define debug
using System;
using System.Collections.Generic;
using UnityEngine;

public class WormHole : EventObj, Receiver<Vector3>,Receiver<KeyValuePair<int,bool>>
{
    #region constField
    [SerializeField, Tooltip("isLock对应的长度")]
    private const int count = 3;
    //玩家，怪物加NPC，触发型道具 分别对应的锁下标
    public const int Player = 0;
    public const int MonsterAndNPC = 1;
    public const int TriggerObj = 2;

    #endregion
    #region field

    [SerializeField, Tooltip("传送目的地")]
    private Vector3 transPos = new Vector3(0, 0, 0);
    [SerializeField, Tooltip("对应物体的锁")]
    private bool[] isLock = new bool[count] { false, false, false };
    [SerializeField,Tooltip("触发方式")]
    private Teleporting teleType = Teleporting.Pass;
    #endregion


    /// <summary>
    /// 改变传送目的地
    /// </summary>
    /// <param name="t">目的地坐标</param>
    public void Reveive(TMessage<Vector3> t)
    {
        transPos = t.Message;
    }
    /// <summary>
    /// 外部调用此函数来修改锁
    /// </summary>
    /// <param name="t"></param>
    public void Reveive(TMessage<System.Collections.Generic.KeyValuePair<int, bool>> t)
    {
        var kvp = t.Message;
#if debug
        if (kvp.Key>2||kvp.Key<0)
        {
            throw new IndexOutOfRangeException("键值对，键的值不合法.应为0-2");
        }

#endif

        isLock[kvp.Key] = kvp.Value;
    }


    /// <summary>
    /// 将物体传送到目的地
    /// </summary>
    private void Transfer(Transform who)
    {
        who.position = transPos;
    }
    

    /// <summary>
    /// 物体接触虫洞的逻辑
    /// </summary>
    /// <param name="other"></param>
    private void OnTriggerEnter(Collider other)
    {
        string tag = other.tag;
        Transform sended = other.transform;
        switch (tag)
        {
            case GameManager.ENTITY:
                if (!isLock[MonsterAndNPC])
                {
                    Transfer(sended);
                }
                break;
            case GameManager.PLAYER:
                if (!isLock[Player])
                {
                    if (teleType==Teleporting.Move)
                    {
                        Transfer(sended);
                    }
                    else if (teleType==Teleporting.Pass)
                    {

                        Pass();
                    }
                    else if (teleType==Teleporting.Trigger)
                    {

                    }
                }
                break;
            case GameManager.TRIGGEROBJ:
                if (!isLock[TriggerObj])
                {
                    Transfer(sended);
                }
                break;
            default:
                break;
        }


        
        
    }
 
    /// <summary>
    /// 通关逻辑
    /// </summary>
    public static void Pass()
    {
        // 过关逻辑。UI，等等
        if (GameManager.Instance.passPanel != null)
        {
            if (GameManager.Instance.Sui != null) Destroy(GameManager.Instance.Sui);
            GameManager.Instance.passPanel.SetActive(true);
        }
        else print("设置过关界面的SetActive");
        //改变得分
        var playerMove = GameManager.Instance.Sui.GetComponent<MoveControl>();
        var player = GameManager.Instance.Player;
        playerMove.enabled = false;//玩家暂停移动
        player.enabled = false;
        //PassManage.Instance.isPassing=false;
    }


    /// <summary>
    /// 虫洞触发方式。
    /// 瞬移，通关，触发（触发新的剧情，）
    /// </summary>
    private enum Teleporting
    {
        Move,Pass,Trigger
    }
}
